package com.smedi.ismedi.personnel.core.port.adapter.persistence.repository.impl;

import com.alibaba.fastjson.JSONObject;
import com.github.tobato.fastdfs.domain.fdfs.StorePath;
import com.smedi.ismedi.cache.starter.util.RedisUtil;
import com.smedi.ismedi.personnel.core.domain.repository.SignatureTemporaryRepository;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @Author: xulinjun
 * @Date: 2021-7-13 10:17
 */
@Slf4j
@Service
@AllArgsConstructor
public class SignatureTemporaryRepositoryImpl implements SignatureTemporaryRepository {
    private final RedisUtil redisUtil;

    @Override
    public List<String> checkSignatureTemporaryIsExist(String pattern) {
        List<String> keys = redisUtil.hasKeyFuzzy(pattern);
        return keys;
    }

    @Override
    public void doInsertTempPath(String key, Object data, Long express) {
        redisUtil.set(key, data, express);
    }

    @Override
    public StorePath getStorePathByKey(String key) {
        Object object = redisUtil.get(key);
        if (object != null) {
            JSONObject jsonObject = JSONObject.parseObject((String) object);
            StorePath storePath = jsonObject.toJavaObject(jsonObject, StorePath.class);

            return storePath;
        } else {
            return null;
        }
    }

    @Override
    public void deleteSignatureTemporary(String key) {
        redisUtil.del(key);
    }
}
